package com.ls.fw.config.zookeeper.utils;

import org.apache.commons.lang.StringUtils;

import com.ls.fw.config.zookeeper.beans.ResultRuok;
import com.ls.fw.config.zookeeper.beans.ResultStat;
import com.ls.fw.config.zookeeper.beans.WordsCommand;
import com.ls.fw.config.zookeeper.beans.ZkServerInfo;
import com.ls.fw.config.zookeeper.enums.ServerModelEnum;
import com.ls.fw.config.zookeeper.enums.ServerStatusEnum;

public class ZkServerUtils {

    public static ZkServerInfo initServerInfo(ZkServerInfo info)
			throws Exception {
		ResultStat resultStat = WordsCommand.stat(info);
		if (resultStat != null) {
			ResultRuok ruok  = WordsCommand.ruok(info);
			if (ruok == null) {
				info.setServerStatusEnum(ServerStatusEnum.OFFLINE);
			}else{
				if (StringUtils.isNotBlank(ruok.getImok())) {
					info.setServerStatusEnum(ServerStatusEnum.ONLINE);
					info.setServerModelEnum(ServerModelEnum.getInstance(resultStat.getMode()));
				}else{
					info.setServerStatusEnum(ServerStatusEnum.EXCEPTIOM);
				}
			}
			info.setConnections(resultStat.getConnections());
			info.setCount(resultStat.getNodeCount());
		}else{
			info.setServerStatusEnum(ServerStatusEnum.OFFLINE);
		}
		return info;
	}
}
